import Vue from 'vue'
import VueRouter from 'vue-router'

const loginUser = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/loginUser.vue')
const homePage = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/homePage.vue')
const welcomeTo = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/welcomeTo.vue')
// import loginUser from '../components/loginUser.vue'
// import homePage from '../components/homePage.vue'
// import welcomeTo from '../components/welcomeTo.vue'

const userList = () => import(/* webpackChunkName: "user" */ '../components/user/userList.vue')
// import userList from '../components/user/userList.vue'

const rightList = () => import(/* webpackChunkName: "power" */ '../components/power/rightList.vue')
const roleList = () => import(/* webpackChunkName: "power" */ '../components/power/roleList.vue')
// import rightList from '../components/power/rightList.vue'
// import roleList from '../components/power/roleList.vue'

const goodsCate = () => import(/* webpackChunkName: "power" */ '../components/goods/goodsCate.vue')
const goodsParams = () => import(/* webpackChunkName: "power" */ '../components/goods/goodsParams.vue')
const goodsList = () => import(/* webpackChunkName: "power" */ '../components/goods/goodsList.vue')
const addGood = () => import(/* webpackChunkName: "power" */ '../components/goods/addGood.vue')
// import goodsCate from '../components/goods/goodsCate.vue'
// import goodsParams from '../components/goods/goodsParams.vue'
// import goodsList from '../components/goods/goodsList.vue'
// import addGood from '../components/goods/addGood.vue'

const orderList = () => import(/* webpackChunkName: "order_report" */ '../components/order/orderList.vue')
const reportList = () => import(/* webpackChunkName: "order_report" */ '../components/report/reportList.vue')
// import orderList from '../components/order/orderList.vue'
// import reportList from '../components/report/reportList.vue'

Vue.use(VueRouter)

const routes = [
  // 访问根路径自动重定向到登录组件
  {
    path: '/',
    redirect: '/loginUser'
  },
  {
    path: '/loginUser',
    component: loginUser
  },
  {
    path: '/homePage',
    component: homePage,
    redirect: '/welcomeTo',
    children: [
      { path: '/welcomeTo', component: welcomeTo },
      { path: '/userList', component: userList },
      { path: '/rightList', component: rightList },
      { path: '/roleList', component: roleList },
      { path: '/goodsCate', component: goodsCate },
      { path: '/goodsParams', component: goodsParams },
      { path: '/goodsList', component: goodsList },
      { path: '/goods/addGood', component: addGood },
      { path: '/orderList', component: orderList },
      { path: '/reportList', component: reportList }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径,from从哪个路径跳转而来，next函数表示放行
  if (to.path === '/loginUser') return next()
  // 从sessionStorage中获取token,没有token就强制跳转登录页面，有则放行
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/loginUser')
  next()
})

export default router
